package com.yilin.platform.base_core.components.redis;

import com.sun.istack.NotNull;
import com.yilin.platform.base_core.commons.utils.MapUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class RedisClient implements ApplicationContextAware {
    private static ApplicationContext currentApplicationContext;
    @Resource
    private StringRedisTemplate template;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        currentApplicationContext = applicationContext;
    }

    public static RedisClient getInstance() {
        return currentApplicationContext.getBean(RedisClient.class);
    }

    //获取所有键值
    public List<String> getAllKeys() {
        Set<String> keys = template.opsForValue().getOperations().keys("*");
        if (keys == null) return new ArrayList<>();
        return new ArrayList<>(keys);
    }

    //获取进程数量
    public int getClientCount() {
        List<RedisClientInfo> infos = template.getClientList();
        return infos == null ? 0 : infos.size();
    }

    //有关Redis服务器的常规信息
    public Map<String, String> getRedisServer() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("server");
        return MapUtil.generateMap(properties);
    }

    //客户端连接部分
    public Map<String, String> getRedisClients() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("clients");
        return MapUtil.generateMap(properties);
    }

    //内存消耗相关信息
    public Map<String, String> getRedisMemory() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("memory");
        return MapUtil.generateMap(properties);
    }

    //RDB和AOF相关信息
    public Map<String, String> getRedisPersistence() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("persistence");
        return MapUtil.generateMap(properties);
    }

    //一般统计
    public Map<String, String> getRedisStats() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("stats");
        return MapUtil.generateMap(properties);
    }

    //主/副本复制信息
    public Map<String, String> getRedisReplication() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("replication");
        return MapUtil.generateMap(properties);
    }

    //CPU消耗统计信息
    public Map<String, String> getRedisCpu() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("cpu");
        return MapUtil.generateMap(properties);
    }

    //Redis命令统计
    public Map<String, String> getRedisCommandstats() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("commandstats");
        return MapUtil.generateMap(properties);
    }

    //Redis群集”部分
    public Map<String, String> getRedisCluster() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("cluster");
        return MapUtil.generateMap(properties);
    }

    //与数据库相关的统计
    public Map<String, String> getRedisKeyspace() {
        Properties properties = template.getRequiredConnectionFactory().getConnection().info("keyspace");
        return MapUtil.generateMap(properties);
    }

    //------------------键值对操作------------------------------
    public Long getExpire(String key) {
        return template.opsForValue().getOperations().getExpire(key, TimeUnit.SECONDS);
    }

    public Long getSize(String key) {
        try {
            return template.opsForValue().size(key);
        } catch (Exception e) {
            return template.opsForHash().size(key);
        }
    }

    public Long delete(List<String> keys) {
        return template.delete(keys);
    }

    public Boolean exists(String key) {
        return template.hasKey(key);
    }

    public void save(String key, String value) {
        template.opsForValue().set(key, value);
    }

    public String get(String key) {
        return template.opsForValue().get(key);
    }

    public void delete(String key) {
        template.delete(key);
    }

    public void expire(String key, int i) {
        template.expire(key, Duration.ofSeconds(i));
    }

    //------------------Hash操作------------------------------

    public String hget(String key, String var) {
        return (String) template.opsForHash().get(key, var);
    }

    public void hset(String key, String field, String value) {
        template.opsForHash().put(key, field, value);
    }

    public void hdel(String key, Object field) {
        template.opsForHash().delete(key, field);
    }

    public Map<Object, Object> hEntries(String key) {
        try {
            return template.opsForHash().entries(key);
        } catch (Exception e) {
            return MapUtil.generate(map -> map.put("value",template.opsForValue().get(key)));
        }
    }

}
